using System;
using System.Collections.Generic;
using Box2DSharp.Common;
using LitJson;
using Testbed.Abstractions;
using Testbed.TestCases;
using UnityEngine;
public class NetManager
{
    public Action<JsonData> onMessage;
    public void login(string uid, Action<JsonData> callback)
    {
        JsonData data = new JsonData();
        data["uid"] = uid;
        SendMessage("login", data, callback);
    }

    public void createRoom(Action<JsonData> callback)
    {
        SendMessage("createRoom", null, callback);
    }



    public void getMyRoom(Action<JsonData> callback)
    {
        SendMessage("getMyRoom", null, callback);
    }

    public void joinRoom(int roomID, Action<JsonData> callback)
    {
        JsonData data = new JsonData();
        data["roomID"] = roomID;
        SendMessage("joinRoom", data, callback);
    }

    public void getFrames(int start, int end, Action<JsonData> callback)
    {
        JsonData data = new JsonData();
        data["start"] = start;
        data["end"] = end;
        SendMessage("getFrames", data, callback);
    }

    public void startGame(Action<JsonData> callback)
    {
        SendMessage("startGame", null, callback);
    }



    public void sendCtrl(List<int> ctrls)
    {
        JsonData data = new JsonData();
        data["ctrls"] = new JsonData();
        for (int i = 0, c = ctrls.Count; i < c; i++)
        {
            data["ctrls"].Add(ctrls[i]);
        }
        SendMessage("ctrls", data, null);
    }

    public void sendfloatCtrl(List<float> ctrls)
    {
        JsonData data = new JsonData();
        data["ctrls"] = new JsonData();
        for (int i = 0, c = ctrls.Count; i < c; i++)
        {
            data["ctrls"].Add(ctrls[i].ToString());
        }
        SendMessage("ctrls", data, null);
    }

    public void sendLongCtrl(List<long> ctrls)
    {
        JsonData data = new JsonData();
        data["ctrls"] = new JsonData();
        for (int i = 0, c = ctrls.Count; i < c; i++)
        {
            data["ctrls"].Add(ctrls[i].ToString());
        }
        SendMessage("ctrls", data, null);
    }

    Dictionary<string, Action<JsonData>> dicResponse = new Dictionary<string, Action<JsonData>>();
    private int MSGID = 0;
    public string SendMessage(string msgID, JsonData data, Action<JsonData> callback)
    {
        JsonData json = new JsonData();
        if (data != null) json["data"] = data;
        if (msgID == "ctrls")
        {
            json["msgID"] = msgID;
        }
        else
        {
            msgID += "." + ++MSGID;
            json["msgID"] = msgID;
            if (callback != null) dicResponse.Add(msgID, callback);
        }
        Debug.Log("send: " + json.ToJson());
        WebSocketHelper.I.Send(json.ToJson());
        return msgID;
    }

    public void OnMessage(string str)
    {
        Debug.Log("receive: " + str);

        JsonData json = JsonMapper.ToObject(str);
        string msgID = (string)json["msgID"];
        if (dicResponse.ContainsKey(msgID))
        {
            Action<JsonData> callback = dicResponse[msgID];
            dicResponse.Remove(msgID);

            callback(json["data"]);
        }
        else
        {
            onMessage(json);
        }
    }
}